package org.dromara.wms.controller;

import java.util.Collection;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.web.core.BaseService;
import org.dromara.system.domain.SysOss;
import org.dromara.system.service.impl.SysOssServiceImpl;
import org.dromara.wms.domain.WmsProduct;
import org.dromara.wms.domain.WmsProductOss;
import org.dromara.wms.service.WmsProductOssServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wms.service.WmsProductServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 产品管理
 *
 * @author wenrui
 * @date 2024-03-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/product")
public class WmsProductController extends BaseService {

    private final WmsProductServiceImpl wmsProductService;
    private final WmsProductOssServiceImpl wmsProductOssService;
    private final SysOssServiceImpl sysOssService;

    /**
     * 查询产品管理列表
     */
    @SaCheckPermission("wms:product:list")
    @GetMapping("/list")
    public TableDataInfo<WmsProduct> list(WmsProduct bo, PageQuery pageQuery) {
        return wmsProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品管理列表
     */
    @SaCheckPermission("wms:product:export")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WmsProduct bo, HttpServletResponse response) {
        List<WmsProduct> list = wmsProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品管理", WmsProduct.class, response);
    }

    /**
     * 获取产品管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wms:product:query")
    @GetMapping("/{id}")
    public R<WmsProduct> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wmsProductService.queryById(id));
    }

    /**
     * 新增产品管理
     */
    @SaCheckPermission("wms:product:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WmsProduct bo) {
        return toAjax(wmsProductService.insertByBo(bo));
    }

    /**
     * 修改产品管理
     */
    @SaCheckPermission("wms:product:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsProduct bo) {
        return toAjax(wmsProductService.updateByBo(bo));
    }

    /**
     * 删除产品管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wms:product:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wmsProductService.deleteWithValidByIds(List.of(ids), true));
    }

  /**
   * 查询产品管理列表
   */
  @SaCheckPermission("wms:product:oss")
  @GetMapping("/oss/list/{id}")
  public List<SysOss> list(@PathVariable("id") Long id) {
    List<WmsProductOss> wmsProductOssList  =  wmsProductOssService.queryById(id);
    if (wmsProductOssList.isEmpty()){
        return null;
    }
    Collection<Long> ossIdCollection = wmsProductOssList.stream()
      .map(WmsProductOss::getOssId) // 将 ossId 字符串转换为 Long 类型
      .toList();
    return sysOssService.selectOssList(ossIdCollection);
  }

  /**
   * pc商城 获取产品管理详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("wms:product:query")
  @GetMapping("/good/{id}")
  public R<WmsProduct> getProductInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long id) {
    return R.ok(wmsProductService.getGoodById(id));
  }
}
